//
//  TTVViewController.m
//  TTVReFix
//
//  Created by raychow(周启睿) on 02/25/2020.
//  Copyright (c) 2020 raychow(周启睿). All rights reserved.
//

#import "TTVViewController.h"
#import <TTVReFix/TTVReFix.h>

typedef NS_ENUM(NSInteger, TTVButtonType) {
    TTVButtonTypeNone = 0,
    TTVButtonTypeReFix = 1,     //热修复
    TTVButtonTypeObject = 2,    //实例方法
    TTVButtonTypeClass = 3,     //类方法
};

@interface TTVViewController ()

@end

@implementation TTVViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    UIButton* btn1 = [self createButton:@"载入热修复" tag:TTVButtonTypeReFix];
    UIButton* btn2 = [self createButton:@"调用实例方法" tag:TTVButtonTypeObject];
    UIButton* btn3 = [self createButton:@"调用类方法" tag:TTVButtonTypeClass];
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    [self.view addSubview:btn3];
    
    CGFloat x = 30;
    CGFloat y = 100;
    CGFloat w = 300;
    CGFloat h = 100;
    btn1.frame = CGRectMake(x, y, w, h);
    btn2.frame = CGRectMake(x, y + h*1, w, h);
    btn3.frame = CGRectMake(x, y + h*2, w, h);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

- (UIButton*)createButton:(NSString*)text tag:(TTVButtonType)tag{
    UIButton* btn = [[UIButton alloc] init];
    btn.tag = tag;
    [btn setTitle:text forState:0];
    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    return btn;
}

- (void)onClick:(UIButton*)btn{
    switch(btn.tag){
        case TTVButtonTypeReFix:{
            //加载本地js文件
            NSString *pathDemo = [[NSBundle bundleForClass:[self class]] pathForResource:@"patch" ofType:@"js"];
//            NSString* pathDemoText = pathDemo;
//            NSString *jsCoreDemo = [[NSString alloc] initWithData:[[NSFileManager defaultManager] contentsAtPath:pathDemoText] encoding:NSUTF8StringEncoding];
            //运行本地修复脚本
            [[TTVReFixManager shareManager] start:pathDemo];
            break;
        }
        case TTVButtonTypeObject:{
            [self testObj:@"TTVButtonTypeObject"];
            break;
        }
        case TTVButtonTypeClass:{
            [TTVViewController testClass:@"TTVButtonTypeClass"];
            break;
        }
        default:{
            break;
        }
    }
}

- (void)testObj:(NSString*)text{
    NSLog(@"原生 实例 方法:%@",text);
}

+ (void)testClass:(NSString*)text{
    NSLog(@"原生 类 方法:%@",text);
}

- (void)objectCall:(NSString*)text{
    NSLog(@"js 调用objectCall 方法:%@",text);
}
+ (void)classCall:(NSString*)text{
    NSLog(@"js 调用 classCall方法:%@",text);
}
@end
